<?php
namespace Admin\Controller;
use Think\Controller;
class RoleController extends AdminController {
    public function index(){
        $Role = M('privilege');//实例化Role对象
        $count = $Role->count();// 查询满足要求的总记录数
        $itemNum = 6; //每页显示的记录数
        $show = c_page($count);

        //page方法分页
        $data = $Role->order('u_time desc, id desc')
            ->page(I('get.p',1) . ',' . $itemNum)
            ->select();

//        if(IS_AJAX){
//            $json['data'] = $data;
//            $json['page'] = $show;
//            $this->ajaxReturn($json);
//        }else{
            $this->assign('title','角色列表');//标题
            $this->assign('data',$data);// 赋值数据集
            $this->assign('page',$show);// 赋值分页输出
            $this->display();
//        }
    }


    public function add(){
        if(IS_POST){
            $data = I('post.');
            $Role = D('privilege');

            $privilege = implode(',', I('privilege'));
            $data['privilege'] = $privilege;

            $Role->setData($data);
            $res = $Role->insertDb();
            if($res){
                $this->success('添加成功',U('index'));
            }else{
                $this->error('添加失败：'.$Role->getError());
            }
        }else{
            $Menu = M('Menu');//实例化AdminUser对象
            $data = $Menu->order('sort asc,u_time desc,id desc')
                ->select();
            $data = get_category($data,1);
//            var_dump($data);exit;

            $this->assign('privilege',$data);
            $this->assign('title','角色添加');//标题
            $this->display();
        }
    }


    //$id 表示URL问号后面的id字段
    public function edit($id = 0){
        if(IS_POST){
            $data = I('post.');
            $privilege = implode(',', I('privilege'));
            $data['privilege'] = $privilege;

            $privilege = D('privilege');

            $privilege->setData($data);
            $res = $privilege->updateDb();





            if($res){
                $this->success('修改成功',U('index'));

                $arrOne = M('AdminUser')->find(session('lh_uid'));
                $m_privilege = M('privilege')->find($arrOne['privilege_id']);
                $wh1 = array();
                $wh1['id'] = array('in',$m_privilege['privilege']);
                $m_privilege = M('menu')->where($wh1)->select();
                session('lh_privilege',$m_privilege);

            }else{
                $this->error('修改失败：'.$privilege->getError());
            }
        }else{
            if($id<0){
                $this->error("参数错误");
            }
            //获取所有权限
            $Menu = M('Menu');//实例化AdminUser对象
            $privilege = $Menu->order('sort asc,u_time desc,id desc')
                ->select();
            $privilege = get_category($privilege,1);

            //获取角色
            $role = M('privilege')->find($id);
            if(!$role){
                $this->error("参数错误");
            }
            $role['privilege'] = explode(',',$role['privilege']);


            $this->assign('privilege',$privilege);
            $this->assign('role',$role);
            $this->assign('title','角色修改');//标题
            $this->display();
        }
    }

    public function delete($id = 0){
        if($id<1){
            $this->error('参数错误,至少选择一条数据');
        }
        if(!is_array($id)){
            $res = M('AdminUser')->field("username")->where(array('privilege_id'=>$id))->select();

            if($res){
                foreach ($res as $v){
                    $arr[] = $v['username'];
                }
                $names = implode(',',$arr);
                $this->error('删除前,请先修改管理员：'. $names .'的角色',U('index'));
            }
            $res = M('privilege')->delete($id);
        }else{
            $wh1['privilege_id'] = array('in',implode(',',$id));

            $res = M('AdminUser')->field("username")->where($wh1)->select();

            if($res){
                foreach ($res as $v){
                    $arr[] = $v['username'];
                }
                $names = implode(',',$arr);
                $this->error('删除前,请先修改管理员：'. $names .'的角色',U('index'));
            }

            $wh['id'] = array('in',implode(',', $id));
            $res = M('privilege')->where($wh)->delete();
        }
        if($res !== false){
            $this->success('删除成功',U('index'));
        }else{
            $this->error('删除失败：',U('index'));
        }
    }
}
?>